package com.lagou;

import com.lagou.service.UserServiceImpl;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class  ServerBootstrap implements CommandLineRunner {

    @Autowired
    private UserServiceImpl userService;

    public static void main(String[] args) throws InterruptedException {
        System.out.println("服务端启动");
        SpringApplication.run(ServerBootstrap.class, args);
    }


    @Override
    public void run(String... args) throws Exception {
        String ip = "127.0.0.1";
        int port = 8888;
        userService.startServer(ip,port);
        //将服务端注册到zookeeper
        //1.在zookeeper上创建临时节点
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
        CuratorFramework client =
                CuratorFrameworkFactory.newClient("192.168.31.227:2181",retryPolicy);
        client.start();
        String path = "/provide/"+ip+":"+port;
        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path);
    }
}
